package checkers3d.presentation;

import java.awt.Point;
import java.util.LinkedList;

/**
 * Provides common functionality required by GUI components. Also includes
 * constants for key codes.
 *
 * @author Ruben Acuna
 */
public class UtilGUI
{
    /*
     * The character number for the backspace.
     */
    static public final int KEY_BACKSPACE = 8;

    /*
     * The character number for the carriage return.
     */
    static public final int KEY_RETURN = 10;

    /*
     * The character number for the excape key.
     */
    static public final int KEY_ESCAPE = 27;

    /*
     * The character number for the space bar.
     */
    static public final int KEY_SPACE = 32;

    /**
     * Computes the location of all components in a list. Computes are arranged
     * in a centered column.
     *
     * @param size The size of the window.
     * @param components        List of components whose position should be calculated.
     */
    static public void computeLayoutCentered(Point size, int axis,
                                             LinkedList<IDrawable> components)
    {
        int totalHeight = 0;
        int bufferY;
        int y;

        //get total height of all components.
        for(IDrawable component : components)
            totalHeight += component.getRenderResource().getHeight();

        //calculate space between each component.
        bufferY = (size.y - totalHeight) / (components.size() + 1);

        y = bufferY;

        for(IDrawable component : components)
        {
            //center each component on the x axis.
            int x = axis - component.getRenderResource().getWidth() / 2;
            
            component.setDrawPosition(new Point (x, y));

            //set the new y location to be after this component plus the buffer.
            y = y + bufferY + component.getRenderResource().getHeight();
        }
    }

    /**
     * Computes the location of all components in a list. Computes are arranged
     * in a spanning row.
     *
     * @param size The size of the window.
     * @param axis The Y axis value on along which to center.
     * @param components        List of components whose position should be calculated.
     */
    static public void computeLayoutSpanning(Point size, int axis,
                                             LinkedList<IDrawable> components)
    {
        int totalWidth = 0;
        int bufferX;
        int x;

        //get total width of all components.
        for(IDrawable component : components)
            totalWidth += component.getRenderResource().getWidth();

        //calculate space between each component.
        bufferX = (size.x - totalWidth) / (components.size() + 1);

        x = bufferX;

        for(IDrawable component : components)
        {
            //center each component on the x axis.
            int y = axis - component.getRenderResource().getHeight() / 2;

            component.setDrawPosition(new Point (x, y));

            //set the new y location to be after this component plus the buffer.
            x = x + bufferX + component.getRenderResource().getWidth();
        }
    }
}